﻿/*
 * Copyright 2007-2008  Lukasz Laszko lukaszlaszko@gmail.com
 * 
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that the copyright notice and this
 * permission notice and warranty disclaimer appear in supporting
 * documentation, and that the name of the author not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * 
 * The author disclaim all warranties with regard to this
 * software, including all implied warranties of merchantability
 * and fitness.  In no event shall the author be liable for any
 * special, indirect or consequential damages or any damages
 * whatsoever resulting from loss of use, data or profits, whether
 * in an action of contract, negligence or other tortious action,
 * arising out of or in connection with the use or performance of
 * this software. 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Pg.BioMedics.SDR.Web.Controls
{
    /// <summary>
    /// Holds information about uploaded files 
    /// </summary>
    internal class UploadedFilesRegister : IDisposable
    {
        #region Fields

        private static UploadedFilesRegister instance;

        private Dictionary<Guid, UploadedFile> uploadedFiles = new Dictionary<Guid, UploadedFile>(); 

        #endregion

        #region Constructors

        /// <summary>
        /// Creates instance of UploadedFilesRegister
        /// </summary>
        private UploadedFilesRegister() { }

        ~UploadedFilesRegister()
        {
            Dispose();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets UpladFilesRegister instance
        /// </summary>
        internal static UploadedFilesRegister Instance
        {
            get
            {
                if (instance == null)
                    instance = new UploadedFilesRegister();

                return instance;
            }
        }

        /// <summary>
        /// Gets uploaded file assigned to the given upload id
        /// </summary>
        /// <param name="uploadId">unique upload id</param>
        /// <returns>Uploaded file info structure</returns>
        internal UploadedFile this[Guid uploadId]
        {
            get
            {
                if (this.uploadedFiles.ContainsKey(uploadId))
                    return this.uploadedFiles[uploadId];
                else
                    return null;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Registers a new file upload
        /// </summary>
        /// <param name="uploadId">file upload unique id</param>
        /// <param name="file">File location</param>
        internal void Register(Guid uploadId, UploadedFile file)
        {
            if (this.uploadedFiles.ContainsKey(uploadId))
            {                
                this.uploadedFiles[uploadId].Dispose();
                this.uploadedFiles.Remove(uploadId);
            }

            this.uploadedFiles.Add(uploadId, file);
        }

        /// <summary>
        /// Removes registered upload
        /// </summary>
        /// <param name="uploadId"></param>
        internal void Unregister(Guid uploadId)
        {
            if (this.uploadedFiles.ContainsKey(uploadId))
            {
                this.uploadedFiles[uploadId].Dispose();
                this.uploadedFiles.Remove(uploadId);
            }

        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            foreach (UploadedFile file in this.uploadedFiles.Values)
                file.Dispose();

            instance = null;
        }

        #endregion
    }
}
